<?php defined('SYSPATH') or die('No direct script access.');

class Searchs_Model extends Model {

	protected $criteria;
	
	public function __construct() {		
		$this->criteria = '';
		$this->db = Database::instance();
	}
	
	public function set_artist_criteria($artist) {
		if (empty($artist)) {
			$this->criteria = "";
		} 
		else {
			$this->criteria = "WHERE a.name LIKE '%". $artist ."%' ";
		}
	} 
	
	public function set_song_criteria($song) {
		if (empty($song)) {
			$this->criteria = "";
		} 
		else {
			$this->criteria = "WHERE s.title LIKE '%". $song ."%' ";
		}
	} 

	public function set_album_criteria($album) {
		if (empty($album)) {
			$this->criteria = "";
		} 
		else {
			$this->criteria = "WHERE ab.title LIKE '%". $album ."%' ";
		}
	} 
	
	public function set_gene_criteria($gene) {
		if (empty($gene) || ($gene=='0')) {
			$this->criteria = "WHERE g.id = 0 ";
		} 
		else {
			$this->criteria = "WHERE g.id = ". $gene . " ";
		}
	} 
	
	public function set_song_hits_criteria($song) {
		if (empty($song)) {
			$this->criteria = "WHERE (r.rating>0)";
		} 
		else {
			$this->criteria = "WHERE s.title LIKE '%". $song ."%' ";
			$this->criteria .= "AND (r.rating>0) ";
		}
	} 
	
	public function set_new_song_criteria($song) {
		if (empty($song)) {
			$this->criteria = "";
		} 
		else {
			$this->criteria = "WHERE s.title LIKE '%". $song ."%' ";
		}
	} 

	public function set_new_channel_criteria($channel) {
		if (empty($channel)) {
			$this->criteria = "";
		} 
		else {
			$this->criteria = "WHERE c.title LIKE '%". $channel ."%' ";
		}
	} 

	public function set_channel_hits_criteria($channel) {
		if (empty($channel)) {
			$this->criteria = "WHERE (r.rating>0)";
		} 
		else {
			$this->criteria = "WHERE c.title LIKE '%". $channel ."%' ";
			$this->criteria .= "AND (r.rating>0) ";
		}
	} 
	
	private function generate_search_by_artist($count=false, $order='1') {
		if (!$count) {
			$sql = '
					SELECT a.id as artist_id, a.name as artist_name, a.artists_picture,
					am.album_id, ab.title as album_name, am.track_no, s.title, g.name as gene, s.duration, 
					s.sample_file, s.low_bit_rate_file, s.high_bit_rate_file, s.created, 
					(SELECT Count(*) FROM profiles_download_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as downloaded,
					(SELECT Count(*) FROM profiles_vote_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as voted
					FROM artists a 
					INNER JOIN artists_albums aa ON a.id=aa.artist_id
					INNER JOIN albums ab ON aa.album_id=ab.id
					INNER JOIN artists_songs am ON a.id=am.artist_id 
					INNER JOIN songs s ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		else {
			$sql = '
					SELECT count(*) as total_song
					FROM artists a 
					INNER JOIN artists_albums aa ON a.id=aa.artist_id
					INNER JOIN albums ab ON aa.album_id=ab.id
					INNER JOIN artists_songs am ON a.id=am.artist_id 
					INNER JOIN songs s ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		$sql .= $this->criteria;
		switch ($order) {
			case '1' :
				$sql .= 'ORDER BY a.name ';
				break;
			case '2' :
				$sql .= 'ORDER BY s.title ';
				break;
			case '3' :
				$sql .= 'ORDER BY ab.title ';
				break;
			case '4' :
				$sql .= 'ORDER BY g.name ';
				break;				
			default :
				$sql .= 'ORDER BY a.name ';
				break;
		}
		return $sql;
		//Kohana::log('debug', Kohana::debug($sql));
	}
	
	private function generate_search_by_song($count=false, $order='1') {
		if (!$count) {
			$sql = '
					SELECT a.id as artist_id, a.name as artist_name, a.artists_picture,
					am.album_id, ab.title as album_name, am.track_no, s.title, g.name as gene, s.duration, 
					s.sample_file, s.low_bit_rate_file, s.high_bit_rate_file, s.created, 
					(SELECT Count(*) FROM profiles_download_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as downloaded,
					(SELECT Count(*) FROM profiles_vote_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as voted
					FROM artists a 
					INNER JOIN artists_albums aa ON a.id=aa.artist_id
					INNER JOIN albums ab ON aa.album_id=ab.id
					INNER JOIN artists_songs am ON a.id=am.artist_id 
					INNER JOIN songs s ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		else {
			$sql = '
					SELECT count(*) as total_song
					FROM artists a 
					INNER JOIN artists_albums aa ON a.id=aa.artist_id
					INNER JOIN albums ab ON aa.album_id=ab.id
					INNER JOIN artists_songs am ON a.id=am.artist_id 
					INNER JOIN songs s ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		$sql .= $this->criteria;
		switch ($order) {
			case '1' :
				$sql .= 'ORDER BY a.name ';
				break;
			case '2' :
				$sql .= 'ORDER BY s.title ';
				break;
			case '3' :
				$sql .= 'ORDER BY ab.title ';
				break;
			case '4' :
				$sql .= 'ORDER BY g.name ';
				break;				
			default :
				$sql .= 'ORDER BY a.name ';
				break;
		}
		return $sql;
		//Kohana::log('debug', Kohana::debug($sql));
	}
	
	private function generate_search_by_album($count=false, $order='1') {
		if (!$count) {
			$sql = '
					SELECT a.id as artist_id, a.name as artist_name, a.artists_picture,
					am.album_id, ab.title as album_name, am.track_no, s.title, g.name as gene, s.duration, 
					s.sample_file, s.low_bit_rate_file, s.high_bit_rate_file, s.created, 
					(SELECT Count(*) FROM profiles_download_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as downloaded,
					(SELECT Count(*) FROM profiles_vote_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as voted
					FROM artists a 
					INNER JOIN artists_albums aa ON a.id=aa.artist_id
					INNER JOIN albums ab ON aa.album_id=ab.id
					INNER JOIN artists_songs am ON a.id=am.artist_id 
					INNER JOIN songs s ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		else {
			$sql = '
					SELECT count(*) as total_song
					FROM artists a 
					INNER JOIN artists_albums aa ON a.id=aa.artist_id
					INNER JOIN albums ab ON aa.album_id=ab.id
					INNER JOIN artists_songs am ON a.id=am.artist_id 
					INNER JOIN songs s ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		$sql .= $this->criteria;
		switch ($order) {
			case '1' :
				$sql .= 'ORDER BY a.name ';
				break;
			case '2' :
				$sql .= 'ORDER BY s.title ';
				break;
			case '3' :
				$sql .= 'ORDER BY ab.title ';
				break;
			case '4' :
				$sql .= 'ORDER BY g.name ';
				break;				
			default :
				$sql .= 'ORDER BY a.name ';
				break;
		}
		return $sql;
		//Kohana::log('debug', Kohana::debug($sql));
	}
	
	private function generate_search_by_gene($count=false, $order='1') {
		if (!$count) {
			$sql = '
					SELECT a.id as artist_id, a.name as artist_name, a.artists_picture,
					am.album_id, ab.title as album_name, am.track_no, s.title, g.name as gene, s.duration, 
					s.sample_file, s.low_bit_rate_file, s.high_bit_rate_file, s.created, 
					(SELECT Count(*) FROM profiles_download_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as downloaded,
					(SELECT Count(*) FROM profiles_vote_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as voted
					FROM artists a 
					INNER JOIN artists_albums aa ON a.id=aa.artist_id
					INNER JOIN albums ab ON aa.album_id=ab.id
					INNER JOIN artists_songs am ON a.id=am.artist_id 
					INNER JOIN songs s ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		else {
			$sql = '
					SELECT count(*) as total_song
					FROM artists a 
					INNER JOIN artists_albums aa ON a.id=aa.artist_id
					INNER JOIN albums ab ON aa.album_id=ab.id
					INNER JOIN artists_songs am ON a.id=am.artist_id 
					INNER JOIN songs s ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		$sql .= $this->criteria;
		switch ($order) {
			case '1' :
				$sql .= 'ORDER BY a.name ';
				break;
			case '2' :
				$sql .= 'ORDER BY s.title ';
				break;
			case '3' :
				$sql .= 'ORDER BY ab.title ';
				break;
			case '4' :
				$sql .= 'ORDER BY g.name ';
				break;				
			default :
				$sql .= 'ORDER BY a.name ';
				break;
		}
		return $sql;
		//Kohana::log('debug', Kohana::debug($sql));
	}
	
	private function generate_search_by_song_hits($count=false, $order='1') {
		if (!$count) {
			$sql = '
					SELECT a.id as artist_id, a.name as artist_name, a.artists_picture,
					am.album_id, ab.title as album_name, am.track_no, s.title, g.name as gene, s.duration, 
					s.sample_file, s.low_bit_rate_file, s.high_bit_rate_file, s.created, 
					(SELECT Count(*) FROM profiles_download_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as downloaded,
					(SELECT Count(*) FROM profiles_vote_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as voted,
					r.rating
					FROM
					(
					    SELECT rr.album_id, rr.track_no, SUM(rr.rating) as rating
					    FROM (
					        SELECT album_id, track_no, count(*) as rating
					        FROM profiles_download_songs
					        GROUP BY album_id, track_no
					        UNION ALL
					        SELECT album_id, track_no, SUM(rating) as rating
					        FROM profiles_vote_songs
					        GROUP BY album_id, track_no
					    ) rr 
					    GROUP BY rr.album_id, rr.track_no
					) r
					INNER JOIN songs s ON (r.album_id=s.album_id) AND (r.track_no=s.track_no)
					INNER JOIN albums ab ON s.album_id=ab.id
					INNER JOIN artists_songs am ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN artists_albums aa ON am.artist_id=aa.artist_id
					INNER JOIN artists a ON aa.artist_id=a.id
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		else {
			$sql = '
					SELECT count(*) as total_song
					FROM
					(
					    SELECT rr.album_id, rr.track_no, SUM(rr.rating) as rating
					    FROM (
					        SELECT album_id, track_no, count(*) as rating
					        FROM profiles_download_songs
					        GROUP BY album_id, track_no
					        UNION ALL
					        SELECT album_id, track_no, SUM(rating) as rating
					        FROM profiles_vote_songs
					        GROUP BY album_id, track_no
					    ) rr 
					    GROUP BY rr.album_id, rr.track_no
					) r
					INNER JOIN songs s ON (r.album_id=s.album_id) AND (r.track_no=s.track_no)
					INNER JOIN albums ab ON s.album_id=ab.id
					INNER JOIN artists_songs am ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN artists_albums aa ON am.artist_id=aa.artist_id
					INNER JOIN artists a ON aa.artist_id=a.id
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		$sql .= $this->criteria;
		switch ($order) {
			case '1' :
				$sql .= 'ORDER BY r.rating DESC ';
				break;
			case '2' :
				$sql .= 'ORDER BY r.rating ASC ';
				break;
			default :
				$sql .= 'ORDER BY r.rating DESC ';
				break;
		}
		return $sql;
		//Kohana::log('debug', Kohana::debug($sql));
	}
	
	private function generate_search_by_new_song($count=false, $order='1') {
		if (!$count) {
			$sql = '
					SELECT a.id as artist_id, a.name as artist_name, a.artists_picture,
					am.album_id, ab.title as album_name, am.track_no, s.title, g.name as gene, s.duration, 
					s.sample_file, s.low_bit_rate_file, s.high_bit_rate_file, s.created, 
					(SELECT Count(*) FROM profiles_download_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as downloaded,
					(SELECT Count(*) FROM profiles_vote_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as voted,
					ab.released_date
					FROM artists a 
					INNER JOIN artists_albums aa ON a.id=aa.artist_id
					INNER JOIN albums ab ON aa.album_id=ab.id
					INNER JOIN artists_songs am ON a.id=am.artist_id 
					INNER JOIN songs s ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		else {
			$sql = '
					SELECT count(*) as total_song
					FROM artists a 
					INNER JOIN artists_albums aa ON a.id=aa.artist_id
					INNER JOIN albums ab ON aa.album_id=ab.id
					INNER JOIN artists_songs am ON a.id=am.artist_id 
					INNER JOIN songs s ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		$sql .= $this->criteria;
		switch ($order) {
			case '1' :
				$sql .= 'ORDER BY ab.released_date DESC, s.album_id, s.track_no ';
				break;
			case '2' :
				$sql .= 'ORDER BY ab.released_date ASC, s.album_id, s.track_no ';
				break;
			default :
				$sql .= 'ORDER BY ab.released_date DESC, s.album_id, s.track_no ';
				break;
		}
		return $sql;
		//Kohana::log('debug', Kohana::debug($sql));
	}
	
	private function generate_search_by_new_channel($count=false, $order='1') {
		if (!$count) {
			$sql = '
					SELECT DISTINCT a.id as artist_id, a.name as artist_name, a.artists_picture,
					am.album_id, ab.title as album_name, am.track_no, s.title, g.name as gene, s.duration, 
					s.sample_file, s.low_bit_rate_file, s.high_bit_rate_file, s.created, 
					(SELECT Count(*) FROM profiles_download_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as downloaded,
					(SELECT Count(*) FROM profiles_vote_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as voted,
					DATE(c.created) as channel_created, c.owner_id, c.id as channel_id, c.title as channel_name,
					(SELECT Count(*) FROM profiles_vote_channels WHERE (owner_id=c.owner_id) AND (channel_id=c.id)) as channel_voted
					FROM channels c 
					INNER JOIN channels_playlists cp ON (c.owner_id=cp.owner_id) AND (c.id=cp.channel_id)
					INNER JOIN playlists_songs ps ON cp.playlist_id = ps.playlist_id
					INNER JOIN songs s ON (ps.album_id=s.album_id) AND (ps.track_no=s.track_no)
					INNER JOIN albums ab ON s.album_id=ab.id
					INNER JOIN artists_songs am ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN artists_albums aa ON am.artist_id=aa.artist_id
					INNER JOIN artists a ON aa.artist_id=a.id
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		else {
			$sql = '
					SELECT DISTINCT count(*) as total_song
					FROM channels c 
					INNER JOIN channels_playlists cp ON (c.owner_id=cp.owner_id) AND (c.id=cp.channel_id)
					INNER JOIN playlists_songs ps ON cp.playlist_id = ps.playlist_id
					INNER JOIN songs s ON (ps.album_id=s.album_id) AND (ps.track_no=s.track_no)
					INNER JOIN albums ab ON s.album_id=ab.id
					INNER JOIN artists_songs am ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN artists_albums aa ON am.artist_id=aa.artist_id
					INNER JOIN artists a ON aa.artist_id=a.id
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		$sql .= $this->criteria;
		switch ($order) {
			case '1' :
				$sql .= 'ORDER BY c.created DESC, c.title, s.album_id, s.track_no ';
				break;
			case '2' :
				$sql .= 'ORDER BY c.created ASC, c.title, s.album_id, s.track_no ';
				break;
			default :
				$sql .= 'ORDER BY c.created DESC, c.title, s.album_id, s.track_no ';
				break;
		}
		return $sql;
		//Kohana::log('debug', Kohana::debug($sql));
	}
	
	private function generate_search_by_channel_hits($count=false, $order='1') {
		if (!$count) {
			$sql = '
					SELECT DISTINCT a.id as artist_id, a.name as artist_name, a.artists_picture,
					am.album_id, ab.title as album_name, am.track_no, s.title, g.name as gene, s.duration, 
					s.sample_file, s.low_bit_rate_file, s.high_bit_rate_file, s.created, 
					(SELECT Count(*) FROM profiles_download_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as downloaded,
					(SELECT Count(*) FROM profiles_vote_songs WHERE (album_id=am.album_id) AND (track_no=am.track_no)) as voted,
					DATE(c.created) as channel_created, c.owner_id, c.id as channel_id, c.title as channel_name, r.rating,
					(SELECT Count(*) FROM profiles_vote_channels WHERE (owner_id=c.owner_id) AND (channel_id=c.id)) as channel_voted
					FROM 
					(
					    SELECT channel_id, Count(*) as rating
					    FROM profiles_vote_channels
					    GROUP BY channel_id
					) r 
					INNER JOIN channels c ON r.channel_id=c.id
					INNER JOIN channels_playlists cp ON (c.owner_id=cp.owner_id) AND (c.id=cp.channel_id)
					INNER JOIN playlists_songs ps ON cp.playlist_id = ps.playlist_id
					INNER JOIN songs s ON (ps.album_id=s.album_id) AND (ps.track_no=s.track_no)
					INNER JOIN albums ab ON s.album_id=ab.id
					INNER JOIN artists_songs am ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN artists_albums aa ON am.artist_id=aa.artist_id
					INNER JOIN artists a ON aa.artist_id=a.id
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		else {
			$sql = '
					SELECT DISTINCT count(*) as total_song
					FROM 
					(
					    SELECT channel_id, Count(*) as rating
					    FROM profiles_vote_channels
					    GROUP BY channel_id
					) r 
					INNER JOIN channels c ON r.channel_id=c.id
					INNER JOIN channels_playlists cp ON (c.owner_id=cp.owner_id) AND (c.id=cp.channel_id)
					INNER JOIN playlists_songs ps ON cp.playlist_id = ps.playlist_id
					INNER JOIN songs s ON (ps.album_id=s.album_id) AND (ps.track_no=s.track_no)
					INNER JOIN albums ab ON s.album_id=ab.id
					INNER JOIN artists_songs am ON (am.album_id=s.album_id) AND (am.track_no=s.track_no)
					INNER JOIN artists_albums aa ON am.artist_id=aa.artist_id
					INNER JOIN artists a ON aa.artist_id=a.id
					INNER JOIN genes g ON s.gene_id=g.id
			';
		}
		$sql .= $this->criteria;
		switch ($order) {
			case '1' :
				$sql .= 'ORDER BY r.rating DESC ';
				break;
			case '2' :
				$sql .= 'ORDER BY r.rating ASC ';
				break;
			default :
				$sql .= 'ORDER BY r.rating DESC ';
				break;
		}
		return $sql;
		//Kohana::log('debug', Kohana::debug($sql));
	}
	
	public function count_search_by_artists($order)
    { 
    	$sql = $this->generate_search_by_artist(true, $order);
    	//Kohana::log('debug', Kohana::debug($sql));
    	$result = $this->db->query($sql)->current()->total_song;
		return $result;
    }

    public function count_search_by_songs($order)
    { 
    	$sql = $this->generate_search_by_song(true, $order);
    	//Kohana::log('debug', Kohana::debug($sql));
    	$result = $this->db->query($sql)->current()->total_song;
		return $result;
    }
    
 	public function count_search_by_albums($order)
    { 
    	$sql = $this->generate_search_by_album(true, $order);
    	//Kohana::log('debug', Kohana::debug($sql));
    	$result = $this->db->query($sql)->current()->total_song;
		return $result;
    }
    
	public function count_search_by_genes($order)
    { 
    	$sql = $this->generate_search_by_gene(true, $order);
    	//Kohana::log('debug', Kohana::debug($sql));
    	$result = $this->db->query($sql)->current()->total_song;
		return $result;
    }
    
 	public function count_search_by_songs_hits($order)
    { 
    	$sql = $this->generate_search_by_song_hits(true, $order);
    	//Kohana::log('debug', Kohana::debug($sql));
    	$result = $this->db->query($sql)->current()->total_song;
		return $result;
    }
    
    public function count_search_by_new_songs($order)
    { 
    	$sql = $this->generate_search_by_new_song(true, $order);
    	//Kohana::log('debug', Kohana::debug($sql));
    	$result = $this->db->query($sql)->current()->total_song;
		return $result;
    }
    
	public function count_search_by_new_channels($order)
    { 
    	$sql = $this->generate_search_by_new_channel(true, $order);
    	//Kohana::log('debug', Kohana::debug($sql));
    	$result = $this->db->query($sql)->current()->total_song;
		return $result;
    }
    
	public function count_search_by_channel_hits($order)
    { 
    	$sql = $this->generate_search_by_channel_hits(true, $order);
    	//Kohana::log('debug', Kohana::debug($sql));
    	$result = $this->db->query($sql)->current()->total_song;
		return $result;
    }
    
    public function browse_search_by_artists($limit, $offset, $order)
    {
    	$sql = $this->generate_search_by_artist(false, $order);
    	$sql .= 'LIMIT ?, ? ';
    	//Kohana::log('debug', Kohana::debug($sql));
		$result = $this->db->query($sql, array($offset, $limit));
		return $result->result(FALSE);
    }
    
	public function browse_search_by_songs($limit, $offset, $order)
    {
    	$sql = $this->generate_search_by_song(false, $order);
    	$sql .= 'LIMIT ?, ? ';
    	//Kohana::log('debug', Kohana::debug($sql));
		$result = $this->db->query($sql, array($offset, $limit));
		return $result->result(FALSE);
    }

	public function browse_search_by_albums($limit, $offset, $order)
    {
    	$sql = $this->generate_search_by_album(false, $order);
    	$sql .= 'LIMIT ?, ? ';
    	//Kohana::log('debug', Kohana::debug($sql));
		$result = $this->db->query($sql, array($offset, $limit));
		return $result->result(FALSE);
    }
    
	public function browse_search_by_genes($limit, $offset, $order)
    {
    	$sql = $this->generate_search_by_gene(false, $order);
    	$sql .= 'LIMIT ?, ? ';
    	//Kohana::log('debug', Kohana::debug($sql));
		$result = $this->db->query($sql, array($offset, $limit));
		return $result->result(FALSE);
    }
    
	public function browse_search_by_songs_hits($limit, $offset, $order)
    {
    	$sql = $this->generate_search_by_song_hits(false, $order);
    	$sql .= 'LIMIT ?, ? ';
    	//Kohana::log('debug', Kohana::debug($sql));
		$result = $this->db->query($sql, array($offset, $limit));
		return $result->result(FALSE);
    }
    
	public function browse_search_by_new_songs($limit, $offset, $order)
    {
    	$sql = $this->generate_search_by_new_song(false, $order);
    	$sql .= 'LIMIT ?, ? ';
    	//Kohana::log('debug', Kohana::debug($sql));
		$result = $this->db->query($sql, array($offset, $limit));
		return $result->result(FALSE);
    }
    
	public function browse_search_by_new_channels($limit, $offset, $order)
    {
    	$sql = $this->generate_search_by_new_channel(false, $order);
    	$sql .= 'LIMIT ?, ? ';
    	Kohana::log('debug', Kohana::debug($sql));
		$result = $this->db->query($sql, array($offset, $limit));
		return $result->result(FALSE);
    }
    
	public function browse_search_by_channel_hits($limit, $offset, $order)
    {
    	$sql = $this->generate_search_by_channel_hits(false, $order);
    	$sql .= 'LIMIT ?, ? ';
    	Kohana::log('debug', Kohana::debug($sql));
		$result = $this->db->query($sql, array($offset, $limit));
		return $result->result(FALSE);
    }
}